var Log = require('../framework/log');
var Messages = require('../core/messages');
function BattleReplyHandler(session,msg){
    var player = session.getPlayer();
    var enemyId = msg.enemyId;
    var result = msg.result;
    var worldChannel = app.getChannelService().getChannel("world");
    if(worldChannel){
        if(!worldChannel.hasPlayer(enemyId)){
            Log.debug("BattleReplyHandler Fail,enemyId = %s,player = %s",enemyId,player.id);
            return;
        }
        var enemyPlayer = worldChannel.getPlayer(enemyId);
        var battle = app.getBattleService().newBattle();
        battle.addBattlePlayer(player);
        battle.addBattlePlayer(enemyPlayer);
        battle.start();

        player.battleId = battle.battleId;
        enemyPlayer.battleId = battle.battleId;

        var selfData = {hp:player.getHp(),name:player.getName(),wuli:player.getWuli(),swordLevel:player.swordLevel,faceId:player.faceId};
        var enemyData = {hp:enemyPlayer.getHp(),name:enemyPlayer.getName(),wuli:enemyPlayer.getWuli(),swordLevel:enemyPlayer.swordLevel,faceId:player.faceId};

        player.getSession().notify(new Messages.BattleStartNotify(enemyId,enemyData,battle.getCards(player.id)));
        enemyPlayer.getSession().notify(new Messages.BattleStartNotify(player.id,selfData,battle.getCards(enemyPlayer.id)));

        enemyPlayer.getSession().notify(new Messages.BattleServerReplyNotify(enemyId,enemyPlayer.getName(),result));
        Log.debug("BattleReplyHandler OK,player = %s",player.id);
        return;
    }else{
        Log.debug("BattleReplyHandler Fail,player = %s",player.id);
        return;
    }
}

module.exports = BattleReplyHandler;